/**
 * 
 */
package com.oscarmovie.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.oscarmovie.Constants;
import com.oscarmovie.WeiboUtils;
import com.oscarmovie.model.Account;
import com.oscarmovie.model.CommentMovie;
import com.oscarmovie.model.CommentPerson;
import com.oscarmovie.model.Movie;
import com.oscarmovie.model.Paging;
import com.oscarmovie.model.Comment;
import com.oscarmovie.model.Statistics;
import com.oscarmovie.service.MovieService;
import com.oscarmovie.service.CommentService;
import com.oscarmovie.service.StatisticsService;

public class CommentAction implements SessionAware{
	
	private CommentMovie commentMovie;
	private CommentPerson commentPerson;
	private CommentService commentService;
	private StatisticsService statisticsService;
	private Paging paging = new Paging(20);
	
	private Movie movie;
	private List<Comment> comments;
	private Map<String, Object> session;
	
	public String addCommentMovie() throws Exception{
		//TODO: should add error handling.
		
		//1. Update statistics
		Statistics statistics = statisticsService.load(commentMovie.getMovie().getStatistics().getId());
		statistics.setCommented(statistics.getCommented()+1);
		statisticsService.save(statistics);
		
		//2. Add comment
		commentMovie.setDatetime(new Date());
		commentService.save(commentMovie);
		
		//3. Send weibo
		if(commentMovie.isSendToWeibo()){
			Account account = session.get("account")==null?null:(Account)session.get("account");
			WeiboUtils.sendWeibo(account.getWeibo(), commentMovie.getContent(), commentMovie.getMovie().getImage().getUrl());
		}
		return Constants.REFRESH;
	}
	public String addCommentPerson() throws Exception{
		//1. Update statistics
		Statistics statistics = statisticsService.load(commentPerson.getPerson().getStatistics().getId());
		statistics.setCommented(statistics.getCommented()+1);
		statisticsService.save(statistics);
		
		//2. Add comment
		commentPerson.setDatetime(new Date());
		commentService.save(commentPerson);
		
		//3. Send weibo
		if(commentPerson.isSendToWeibo()){
			Account account = session.get("account")==null?null:(Account)session.get("account");
			WeiboUtils.sendWeibo(account.getWeibo(), commentPerson.getContent(), commentPerson.getPerson().getImage().getUrl());
		}
		return Constants.REFRESH;
	}
	public String list() throws Exception {
		comments = commentService.listByMovieId(movie.getId(), paging);
		return Constants.REFRESH;
	}

	

	public void setCommentService(CommentService commentService) {
		this.commentService = commentService;
	}


	public CommentService getCommentService() {
		return commentService;
	}


	public void setPaging(Paging paging) {
		this.paging = paging;
	}


	public Paging getPaging() {
		return paging;
	}


	public void setComments(List<Comment> comments) {
		this.comments = comments;
	}

	public List<Comment> getComments() {
		return comments;
	}

	public void setMovie(Movie movie) {
		this.movie = movie;
	}

	public Movie getMovie() {
		return movie;
	}

	public void setCommentPerson(CommentPerson commentPerson) {
		this.commentPerson = commentPerson;
	}

	public CommentPerson getCommentPerson() {
		return commentPerson;
	}

	public void setCommentMovie(CommentMovie commentMovie) {
		this.commentMovie = commentMovie;
	}

	public CommentMovie getCommentMovie() {
		return commentMovie;
	}
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	public void setStatisticsService(StatisticsService statisticsService) {
		this.statisticsService = statisticsService;
	}
	public StatisticsService getStatisticsService() {
		return statisticsService;
	}
	

	
}
